package conceptmaniax.hotelapp.util.geo;

import android.location.Criteria;
import android.location.Location;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.maps.GeoPoint;

public final class GeoUtils {

	private static final double EARTH_RADIUS = 3958.75;
	private static final int METER_CONVERSION = 1609;
	
	private GeoUtils() {
		super();
	}
	
	// Returns distance in meters
	public static double distance(final double lat1, final double lon1, final double lat2, final double lon2) {
	    final double dLat = Math.toRadians(lat2-lat1);
	    final double dLng = Math.toRadians(lon2-lon1);
	    final double distTmp1 = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng/2) * Math.sin(dLng/2);
	    final double distTmp2 = 2 * Math.atan2(Math.sqrt(distTmp1), Math.sqrt(1-distTmp1));
	    
	    return (float) (EARTH_RADIUS * distTmp2 * METER_CONVERSION); 
	}
	
	public static double calculateDistance(final Location location, final double itemLatidute, final double itemLongtidute) {
		if ( location != null ) {
			return GeoUtils.distance(location.getLatitude(), location.getLongitude(), itemLatidute, itemLongtidute);
		} else {
			return 0.0d;
		}
	}
	
	public static String printDistance(final double distance) {
		if ( distance > 1000 ){
			return Double.toString((double)Math.round((distance / 1000) * 10) / 10) +"km";
		} else {
			return Double.toString((double)Math.round(distance * 10) / 10) +"m";
		}
	}
	
	public static GeoPoint getGeoPoint(final String latidute, final String longtidute) {
		final double itemLatidute = Double.parseDouble(latidute);
	    final double itemLongtidute = Double.parseDouble(longtidute);
	    return new GeoPoint((int) (itemLatidute * 1E6), (int) (itemLongtidute * 1E6));
	}
	
	public static GeoPoint getGeoPoint(final double latidute, final double longtidute) {
	    return new GeoPoint((int) (latidute * 1E6), (int) (longtidute * 1E6));
	}
	
	public static Criteria getCriteria() {
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		criteria.setSpeedRequired(false);
		criteria.setBearingRequired(false);
		criteria.setAltitudeRequired(false);
		return criteria;
	}
	
	public static LatLng getLatLng(final String latidute, final String longtidute) {
		final double itemLatidute = Double.parseDouble(latidute);
	    final double itemLongtidute = Double.parseDouble(longtidute);
	    return new LatLng(itemLatidute, itemLongtidute);
	}
	
	public static LatLng getLatLng(final double latidute, final double longtidute) {
	    return new LatLng(latidute, longtidute);
	}
}
